<?php

class calendar
{
    private $previous_year = 2018;
    private $years_total = 20;
    private $day_count = -31;

    function calendar_years()
    {
        $html_years = "";
        # $html_year = array();
        for ($i = 1; $i < $this->years_total; $i++)  {
            $html_years .= "<div style=\"border-top: 1px solid #f00\"></div>";
            $html_years .= self::calendar_months($i);
            # $html_year[] = self::calendar_leap_year($i);
        }
        return $html_years;
    }

    function calendar_months($year_nm)
    {
        $html_months = "";
        $big_months = array(1, 3, 5, 7, 8, 10, 12);
        $small_months = array(4, 6, 9, 11);
        $year_months = array(2);
        $day = $this->day_count;
        $leap_days = self::calendar_leap_year($year_nm)['is_leap'];
        for ($i = 1; $i < 13; $i++) {
            $html_months .= "<hr style=\"border-top: 1px solid #0f0\">";
            $days = 31;
            if (in_array($i, $small_months)) {
                $days = 30;
            } elseif (in_array($i, $year_months)) {
                $days = 28;
                if (1 == $leap_days) {
                    $days = 29;
                }
            }
            # $arr_days = calendar_days($days, $i, $year_nm)[0];
            $day += $days;
            $html_months .= self::calendar_days($days, $i, $year_nm, $day)[1];
        }
        return $html_months;
    }

    function calendar_days($days, $month_nm, $year_nm, $day_no = 0, $day_number = 0)
    {
        $days++;
        $html_days = "<table style=\"border-top: 1px solid #00f; width: 100%\">";
        for ($i = 28; $i < $days; $i++) {
            $year_name = $this->previous_year + $year_nm;
            $day_no++;
            $html_days .= "<tr><td> $day_number </td><td> $day_no </td><td> $year_nm </td><td> $year_name </td><td> $month_nm </td><td> $i </td></tr>";
        }
        $html_days .= "</table>";
        return array($i, $html_days);
    }

    function calendar_weeks()
    {

    }

    function calendar_leap_year($year)
    {
        $is_leap = (1 == $year % 4) ? 1 : 0;
        $not_leap = (1 == $year % 100) ? 1 : 0;
        # $leap = 
        return get_defined_vars();
    }
}

$calendar = new calendar();
echo $years = $calendar->calendar_years();exit;
$html = "";
foreach ($years as $year_key => $year_value) {
    extract($year_value);
    $html .= "<dl><dt>$year_key</dt><dd>$is_leap $not_leap</dd></dl>";
}
echo $html;
